package org.hmh.offer;

import java.util.Stack;

/**
 * Created this one by HMH on 2018/1/20.
 */
public class t14 {
    static public class Solution {

        Stack<Integer> stack;
        int min;

        public Solution() {
            stack = new Stack<>();
            min = 9999999;
        }

        public void push(int node) {

            if (stack.size() == 0) {
                stack.push(0);
                min = node;
                return;
            }
            if (node < min) {
                stack.push(node - min);
                min = node;
                return;
            }
            stack.push(node - min);
        }

        public void pop() {
            int top = stack.peek();
            if (top <= 0) {
                min = min - top;
            }
            stack.pop();
        }

        public int top() {
            return min + stack.peek();
        }

        public int min() {
            return min;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.push(3);
        solution.push(4);
        solution.push(2);
        solution.push(2);
        System.out.println(solution.min());
        solution.pop();
        System.out.println(solution.min());
        solution.pop();
        System.out.println(solution.min());
    }

}
